package cn.jjxx.modules.workflow.service.impl;

import cn.jjxx.core.common.service.impl.TreeCommonServiceImpl;
import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.modules.workflow.mapper.ActReTypeMapper;
import cn.jjxx.modules.workflow.entity.ActReType;
import cn.jjxx.modules.workflow.service.IActReTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**   
 * @Title: 流程类型表
 * @Description: 流程类型表
 * @author jjxx
 * @date 2017-12-14 20:59:03
 * @version V1.0   
 *
 */
@Transactional
@Service("actReTypeService")
public class ActReTypeServiceImpl  extends TreeCommonServiceImpl<ActReTypeMapper,ActReType,String> implements  IActReTypeService {

	@Override
	public boolean insertOrUpdate(ActReType entity) {
		if (!ObjectUtils.isNullOrEmpty(entity.getParentId())) {
			ActReType parent = selectById(entity.getParentId());
			updateSelftAndChild(entity, parent.getId(), parent.makeSelfAsNewParentIds());
		} else {
			entity.setParentId(null);
			updateSelftAndChild(entity, null, null);
		}
		return true;
	}
	
	/**
	 * @Description: 更新所有孩子节点的路径 .<br>
	 * @param entity
	 * @param newParentId
	 * @param newParentIds .<br>   
	 * @author 郑成功 .<br>
	 * @date 2018-7-18 上午9:44:13.<br>
	 */
	private void updateSelftAndChild(ActReType entity, String newParentId, String newParentIds) {
		ActReType oldEntity = selectById(entity.getId());
		String oldChildrenParentIds = oldEntity.makeSelfAsNewParentIds();
		String oldChildrenNamePath = oldEntity.getNamePath();
		entity.setParentId(newParentId);
		entity.setParentIds(newParentIds);
		super.insertOrUpdate(entity);
		String newChildrenParentIds = entity.makeSelfAsNewParentIds();
		String newChildrenNamePath = entity.getNamePath();
		baseMapper.updateSunTreeParentIds(newChildrenParentIds, oldChildrenParentIds);
		baseMapper.updateSunTreeNamePath(oldChildrenParentIds,newChildrenNamePath, oldChildrenNamePath);
	}
}
